package io.mqttpush.mqttserver.handles;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


/**
 * @author tianzhenjiu
 * @date 2020/4/27 16:21
 */
public class PingService  implements Iservice<MqttMessage>{

    Logger logger = LoggerFactory.getLogger(PingService.class);
    @Override
    public boolean accept(ChannelHandlerContext context, MqttMessage inMessage) {
        return MqttMessageType.PINGREQ==inMessage.fixedHeader().messageType();
    }

    @Override
    public  List<Object> service(ChannelHandlerContext context, List<Object> outMessage, MqttMessage inMessage){


        Channel channel=context.channel();
        outMessage.add( pong(channel));

        return outMessage;
    }

    /**
     * 处理心跳ping
     *
     * @param channel
     */
    private MqttMessage pong(Channel channel) {

        return new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0));
    }
}
